package com.deyuanyun.pic.settings.service.base.impl;

import com.deyuanyun.pic.settings.domain.base.Code;
import com.deyuanyun.pic.settings.domain.base.CodeExample;
import com.deyuanyun.pic.settings.mapper.base.CodeMapper;
import com.deyuanyun.pic.settings.service.base.CodeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created by xtt on 2015/8/5.
 */
@Service
public class CodeServiceImpl implements CodeService {
    @Resource
    private CodeMapper codeMapper;

    public int deleteByPrimaryKey(String id) {
        return codeMapper.deleteByPrimaryKey(id);
    }

    public int insert(Code record) {
        return codeMapper.insert(record);
    }

    public int insertSelective(Code record) {
        return codeMapper.insertSelective(record);
    }

    public Code selectByPrimaryKey(String id) {
        return codeMapper.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Code record) {
        return codeMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Code record) {
        return codeMapper.updateByPrimaryKey(record);
    }

    public Page<Code> selectList(Code record) {
        return codeMapper.selectList(record);
    }

    public int deleteByIdPath(String idPath) {
        return codeMapper.deleteByIdPath(idPath);
    }

    public Page<Code> selectByIdPath(String idPath) {
        return codeMapper.selectByIdPath(idPath);
    }

    public List<Code> loadAll() {
        CodeExample example = new CodeExample();
        example.createCriteria().andDeletedEqualTo((byte) 0).andStateEqualTo((byte) 0);
        List<Code> codeList = codeMapper.selectByExample(example);
        return codeList;//codeMapper.loadAll();
    }

    @Override
    public List<HashMap> selectList(String majorCode) {
        return codeMapper.selectListAll(majorCode);
    }

    public List<Code> getCodeList(String majorCode) {
        return codeMapper.queryCodeList(majorCode);
    }

    public Code getGwNCode(String sn, String majorCode){
        PageHelper.startPage(1, 1); //取第一条
        CodeExample codeExample = new CodeExample();
        codeExample.setOrderByClause("version desc");
        codeExample.createCriteria().andDeletedEqualTo(Byte.valueOf("0")).andMinorCodeEqualTo(sn).andMajorCodeEqualTo(majorCode);
        List<Code> codes = codeMapper.selectByExample(codeExample);
        if(codes != null && !codes.isEmpty()) {
            return codes.get(0);
        }
        return null;
    }

}
